package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class UserDao {
	private DBHelp<User> db=new DBHelp<User>();
	public User findByNameAndPassword(User user){
		
		final String sql="select id,name,password from t_user where name=? and password=?";
		return db.executeQueryForObject(sql,new UserRowMapper(),user.getName(),user.getPassword());
	}
	
	public List<User> findAll(){
		
		final String sql="select id,name,password ,pic from t_user";
		return db.executeQueryForList(sql, new UserRowMapper());
	}
	public List<User> findByProjectId(String ProjectId){
	final String sql="SELECT id,`name` FROM t_user WHERE id IN(SELECT t_user_id FROM t_user_project WHERE t_project_id = ?)";
		return db.executeQueryForList(sql,new UserRowMapper(),ProjectId);
	}
	
	
	
	private class UserRowMapper implements RowMapper<User> {

		public User mapperRow(ResultSet rs) throws SQLException {
			
			User user=new User();
			user.setId(rs.getString("id"));
			user.setPassword(rs.getString("password"));
			user.setName(rs.getString("name"));
			//user.setPic(rs.getString("pic"));
			return user;
			
		}
	
	}


	public User findByName(String manager) {
		final String sql = "select id,name,password from t_user where name=?";
		return db.executeQueryForObject(sql, new UserRowMapper(), manager);
	}
	public void save(User user) {
		final String sql="insert into t_user(id,name,password,pic)values(?,?,?,?)";
		db.executeSQL(sql,user.getId(),user.getName(),user.getPassword(),user.getPic());
	}
	public User findById(String userid) {
		String sql="select * from t_user where id=?";
		return db.executeQueryForObject(sql, new UserRowMapper(), userid);
	}

}
